function [imgFV, sae] = fvLearnSAE( img, rf, params)

img = double(img);
[H,W,band] = size(img);

if max(max(max(img))) > 1 || min(min(min(img))) <0
    disp('Input image must scale to 0~1')
    return
end

    deep = 3;
    numTrain = 10000;
    numLayerNodes = [band 100 100 50];
    for i=1:deep
        sae.ae{i}.activation_function    = 'sigm';
        sae.ae{i}.learningRate       = 1;
        sae.ae{i}.inputZeroMaskedFraction   = 0.8;
    end
    opts.numepochs =   100;
    opts.batchsize = 500;

  if nargin==3
    deep = length( params.numLayerNodes );
    numTrain = params.numTrain-mod(params.numTrain,500);
    numLayerNodes = [band*rf*rf params.numLayerNodes];
    for i=1:deep
            sae.ae{i}.activation_function    = 'sigm';
            sae.ae{i}.learningRate       = 1;
            sae.ae{i}.inputZeroMaskedFraction   = 0.8;
    end
    opts.numepochs =   params.numIters;
    opts.batchsize = 500;  
    else if nargin>3
            disp('Input parameters wong');
            return;
        end
  end
  
train_x = SamplePatches(img, rf, numTrain); % NxD
sae = saesetup( numLayerNodes );
sae = saetrain(sae, train_x, opts);

fvDim = numLayerNodes( length(numLayerNodes) );
imgFV = zeros(H,W,fvDim);

tmpImg = Img2PImg(img, rf);
for i=1:H
    tmpData = squeeze( tmpImg(i,:,:) ) ;
    tmpFV = UpSAE(tmpData', sae);
    %whos 
    imgFV(i,:,:) = tmpFV';
end


end